using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class ManagerToRole:Base
    {

        private DAL.ManagerToRole dal = null;

        public ManagerToRole()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal = new DAL.ManagerToRole(isession);
        }

        public Common.Result Add(string UserName, string RoleCode)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            var role = new DAL.ManagerRole(isession).GetModel(RoleCode);
            if (role == null)
            {
                ret.message = "角色不存在";
                return ret;
            }
            var user = new DAL.Manager(isession).GetModel(UserName);
            if (user == null)
            {
                ret.message = "用户不存在";
                return ret;
            }
            var model = new Model.ManagerToRole()
            {
                UserName = UserName,
                RoleCode = RoleCode
            };
            if (dal.Get(UserName, RoleCode) == null) ret = dal.Add(model);
            return ret;
        }

        public Common.Result AddUser(string UserName, string RoleCode)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");           
            var role = new DAL.ManagerRole(isession).GetModel(RoleCode);
            if (role == null)
            {
                ret.message = "角色不存在";
                return ret;
            }
            var cnt = 0;
            var userdal = new DAL.Manager(isession);
            foreach (var u in UserName.Split(','))
            {
                if (userdal.GetModel(u) != null && dal.Get(u, RoleCode) == null)
                {
                    var model = new Model.ManagerToRole()
                    {
                        UserName = u,
                        RoleCode = RoleCode
                    };
                    ret = dal.Add(model);
                    if (ret.success) cnt++;
                }
            }
            if (cnt > 0)
            {
                ret.success = true;
                ret.count = cnt;
                ret.message = "成功添加 " + cnt + " 条数据";
            }
            return ret;
        }

        public Common.Result AddRole(string UserName, string RoleCode)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");


            var user = new DAL.Manager(isession).GetModel(UserName);
            if (user == null)
            {
                ret.message = "用户不存在";
                return ret;
            }
            var cnt = 0;
            var roledal = new DAL.ManagerRole(isession);
            foreach (var ro in RoleCode.Split(','))
            {
                if (roledal.GetModel(ro)!=null&& dal.Get(UserName, ro) == null)
                {
                    var model = new Model.ManagerToRole()
                    {
                        UserName = UserName,
                        RoleCode = ro
                    };
                    ret = dal.Add(model);
                    if (ret.success) cnt++;
                }
            }
            if (cnt > 0)
            {
                ret.success = true;
                ret.count = cnt;
                ret.message = "成功添加 " + cnt + " 条数据";
            }
            return ret;
        }
        
        public Common.Result Delete(string ids)
        {
            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.ManagerToRole Get(int id)
        {
            return dal.GetModel(id);
        }

        public IList<Model.ManagerToRole> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length, ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }
    }
}
